<?php

namespace common\models\work;

use Yii;

/**
 * This is the model class for table "{{%company}}".
 *
 * @property int $id
 * @property int $parent_id 上级单位ID
 * @property string $name 单位名称
 * @property string $abbr 单位简称
 * @property int $type 单位类型
 * @property string $province 省
 * @property string $city 市
 * @property string $dist 区/县
 * @property string $mark 备注
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class Company extends \common\db\ActiveRecord {

    /**
     * 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%company}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['parent_id', 'type', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'required'],
            [['name'], 'string', 'max' => 100],
            [['abbr', 'province', 'city', 'dist'], 'string', 'max' => 50],
            [['mark'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'parent_id' => '上级单位ID',
            'name' => '单位名称',
            'abbr' => '单位简称',
            'type' => '单位类型',
            'province' => '省',
            'city' => '市',
            'dist' => '区/县',
            'mark' => '备注',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getParent() {
        return $this->hasOne(static::class, ['id' => 'parent_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getChildren() {
        return $this->hasMany(static::class, ['parent_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getActiveChildren() {
        return $this->getChildren()->active();
    }

}
